package com.jxdinfo.hussar.cloud.module.example.tjjc.towertimecount.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jxdinfo.hussar.cloud.module.example.tjjc.towercrane.dao.TowerCraneMapper;
import com.jxdinfo.hussar.cloud.module.example.tjjc.towercrane.model.TowerCrane;
import com.jxdinfo.hussar.cloud.module.example.tjjc.towertimecount.dao.TowerTimeCountMapper;
import com.jxdinfo.hussar.cloud.module.example.tjjc.towertimecount.model.TowerTimeCount;
import com.jxdinfo.hussar.cloud.module.example.tjjc.towertimecount.service.TowerTimeCountService;
import com.jxdinfo.hussar.cloud.module.example.tjjc.towertimecount.vo.TowerTimeCountChartVO;
import com.jxdinfo.hussar.cloud.module.example.tjjc.towertimecount.vo.TowerTimeCountSearchVO;
import com.jxdinfo.hussar.cloud.module.example.tjjc.towertimecount.vo.TowerTimeCountVO;
import com.jxdinfo.hussar.platform.core.base.apiresult.ApiResponse;
import com.jxdinfo.hussar.platform.core.utils.BeanUtil;
import com.jxdinfo.hussar.support.datasource.annotations.HussarDs;
import com.jxdinfo.hussar.support.mp.base.service.impl.HussarServiceImpl;
import org.apache.commons.lang3.ObjectUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 塔机工时统计服务实现类
 */
@Service("tjjc.towertimecount.TowerTimeCountServiceImpl")
@HussarDs("master")
public class TowerTimeCountServiceImpl extends HussarServiceImpl<TowerTimeCountMapper, TowerTimeCount> implements TowerTimeCountService {

    private static final Logger logger = LoggerFactory.getLogger(TowerTimeCountServiceImpl.class);

    private static final String RETURN_CODE = "0";

    @Autowired
    private TowerCraneMapper towerCraneMapper;
    @Autowired
    private TowerTimeCountMapper towerTimeCountMapper;

    @Override
    public ApiResponse<List<TowerTimeCountChartVO>> findByChart(TowerTimeCountSearchVO towerTimeCountSearchVO) {
        QueryWrapper<TowerCrane> wrapper = new QueryWrapper<>();
        wrapper.eq("ty_project_code", towerTimeCountSearchVO.getTyProjectCode());
        wrapper.eq("ty_delete_status", 0);
        if(ObjectUtils.isNotEmpty(towerTimeCountSearchVO.getTowerId())) {
            String [] towerIds = towerTimeCountSearchVO.getTowerId().split(",");
            wrapper.in("id", towerIds);
        }
        List<TowerCrane> towers = towerCraneMapper.selectList(wrapper);

        //封装查询条件
        QueryWrapper<TowerTimeCount> countQueryWrapper = new QueryWrapper<>();
        countQueryWrapper.eq("ty_project_code", towerTimeCountSearchVO.getTyProjectCode());
        if(ObjectUtils.isNotEmpty(towerTimeCountSearchVO.getWorkStartTime())) {
            countQueryWrapper.ge("work_start_time", towerTimeCountSearchVO.getWorkStartTime());
            ZoneId zoneId = ZoneId.systemDefault();
            Date endTime = DateUtil.endOfDay(Date.from(towerTimeCountSearchVO.getWorkStartTime().atZone(zoneId).toInstant()));
            countQueryWrapper.le("work_start_time", endTime);
        }

        List<String> towerIdList = towers.stream().map(TowerCrane::getId).collect(Collectors.toList());
        countQueryWrapper.in("tower_id", towerIdList);

        countQueryWrapper.orderByAsc("work_start_time");

        List<TowerTimeCount> list = towerTimeCountMapper.selectList(countQueryWrapper);
        List<TowerTimeCountVO> towerTimeCountList = BeanUtil.copyToList(list, TowerTimeCountVO.class);
        Map<String, List<TowerTimeCountVO>> map = towerTimeCountList.stream().collect(Collectors.groupingBy(data -> data.getTowerId()));

        List<TowerTimeCountChartVO> dataList = new ArrayList<>();

        for(TowerCrane tower : towers){
            TowerTimeCountChartVO chartDto = new TowerTimeCountChartVO();
            chartDto.setTowerId(tower.getId());
            chartDto.setTowerName(tower.getTowerName());
            List<TowerTimeCountVO> towerTimeCountDtoList = map.get(tower.getId());
            if(towerTimeCountDtoList == null) {
                towerTimeCountDtoList = new ArrayList<>();
            }
            chartDto.setChartDataList(towerTimeCountDtoList);
            dataList.add(chartDto);
        }

        return ApiResponse.success(dataList);
    }

    @Override
    public ApiResponse<List<TowerTimeCountVO>> findByList(TowerTimeCountSearchVO towerTimeCountSearchVO) {
        QueryWrapper<TowerTimeCount> wrapper = new QueryWrapper<>();
        if(ObjectUtils.isNotEmpty(towerTimeCountSearchVO.getTyProjectCode())) {
            wrapper.eq("ty_project_code", towerTimeCountSearchVO.getTyProjectCode());
        }
        if(ObjectUtils.isNotEmpty(towerTimeCountSearchVO.getWorkStartTime())) {
            wrapper.ge("work_start_time", towerTimeCountSearchVO.getWorkStartTime());
        }
        if(ObjectUtils.isNotEmpty(towerTimeCountSearchVO.getWorkEndTime())) {
            wrapper.le("work_start_time", towerTimeCountSearchVO.getWorkEndTime());
        }

        List<TowerTimeCount> list = towerTimeCountMapper.selectList(wrapper);

        List<TowerTimeCountVO> towerTimeCountVOS = BeanUtil.copyToList(list, TowerTimeCountVO.class);
        return ApiResponse.success(towerTimeCountVOS);
    }
}
